package com.ss.android.common.ui.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.browser.R;

public final class SuperToast {
    Context mContext;
    public View b;
    long c;
    public int d = 0;
    public WindowManager e;
    WindowManager.LayoutParams f;
    public Handler mHandler = new Handler(Looper.getMainLooper()) {

        public final void handleMessage(Message arg6) {
            int v1 = -2;
            if (arg6.what == 0) {
                if (SuperToast.this.b == null) {
                    return;
                }

                if (SuperToast.this.b.getParent() == null) {
                    return;
                }

                try {
                    SuperToast.this.e.removeView(SuperToast.this.b);
                } catch (Throwable throwable) {
                    Logger.e("SuperToast", "Failed to hide super toast.", throwable);
                }

                return;
            }

            if (arg6.what == 1) {
                if (SuperToast.this.f == null) {
                    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                    layoutParams.height = v1;
                    layoutParams.width = v1;
                    layoutParams.format = -3;
                    layoutParams.windowAnimations = 16973828;
                    layoutParams.type = 2002;
                    layoutParams.flags = 152;
                    layoutParams.setTitle("Toast");
                    if (SuperToast.this.d == 0) {
                        layoutParams.gravity = 81;
                        layoutParams.y = ((int) TypedValue.applyDimension(1, 80f, SuperToast.this.mContext.getResources().getDisplayMetrics()));
                    } else {
                        layoutParams.gravity = SuperToast.this.d;
                    }

                    SuperToast.this.f = layoutParams;
                }

                try {
                    SuperToast.this.e = (WindowManager) SuperToast.this.mContext.getSystemService(Context.WINDOW_SERVICE);
                    if (SuperToast.this.e == null) {
                    } else {
                        SuperToast.this.e.addView(SuperToast.this.b, SuperToast.this.f);
                    }
                } catch (WindowManager.BadTokenException badTokenException) {
                    Logger.e("SuperToast", "Failed to show super toast.", badTokenException);
                    SuperToast.a(Boolean.valueOf(false));
                    SharedPreferences.Editor editor = SuperToast.getSp(SuperToast.this.mContext).edit();
                    editor.putBoolean("super_toast_enabled", false);
                    editor.apply();
                }

                if (SuperToast.this.c <= 0) {
                    return;
                }
                SuperToast.this.mHandler.sendEmptyMessageDelayed(0, SuperToast.this.c);
            }
        }
    };
    private static Boolean superToast = null;

    private SuperToast(Context arg3) {
        this.mContext = arg3;
    }

    public static SuperToast makeText(Context context, CharSequence text, long duration) {
        SuperToast v1 = new SuperToast(context);
        View v0 = View.inflate(context, R.layout.popup_toast, null);
        v0.setBackgroundResource(com.ss.android.article.browser.R.drawable.bg_slide_hint);
        v0.findViewById(com.ss.android.article.browser.R.id.icon).setVisibility(View.GONE);
        v1.b = v0;
        if(v1.b == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }

        v0 = v1.b.findViewById(com.ss.android.article.browser.R.id.text);
        if(v0 == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }

        ((TextView)v0).setText(text);
        if(duration == 0) {
            v1.c = 2000;
        }
        else if(duration == 1) {
            v1.c = 3500;
        }
        else {
            v1.c = duration;
        }

        return v1;
    }

    static Boolean a(Boolean arg0) {
        superToast = arg0;
        return arg0;
    }

    public static boolean a(Context context) {
        if(superToast == null) {
            superToast = Boolean.valueOf(context.getSharedPreferences("sp_super_toast", 0)
                    .getBoolean("sp_super_toast", true));
        }

        return superToast.booleanValue();
    }

    public static SharedPreferences getSp(Context context) {
        return context.getSharedPreferences("sp_super_toast", 0);
    }
}

